package com.wanmait.hotelManage.webservlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wanmait.hotelManage.dao.BreakfastDAO;
import com.wanmait.hotelManage.dao.CarDAO;
import com.wanmait.hotelManage.dao.OrderDAO;
import com.wanmait.hotelManage.dao.RoomTypeDAO;
import com.wanmait.hotelManage.dao.UserinfoDAO;
import com.wanmait.hotelManage.vo.Breakfast;
import com.wanmait.hotelManage.vo.Order;
import com.wanmait.hotelManage.vo.Pager;
import com.wanmait.hotelManage.vo.Userinfo;

/**
 * Servlet implementation class OrderServlet
 */
@WebServlet("/OrderServlet")
public class OrderServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public OrderServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String action=request.getParameter("action");
		if("order".equals(action)) {
			this.order(request, response);
		}
		else if("myorder".equals(action)) {
			this.myOrder(request, response);
		}
		else if("tui".equals(action)) {
			this.tui(request, response);
		}else if("readyOrder".equals(action)) {
			this.readyOrder(request,response);
		}
		else if("comment".equals(action)) {
			this.comment(request, response);
		}
		else if("de".equals(action)) {
			this.de(request, response);
		}
		
		
	}
	private void de(HttpServletRequest request, HttpServletResponse response) throws IOException 
	{
		int id=Integer.parseInt(request.getParameter("id"));
		OrderDAO.getOrderdao().updremove(id);
	}
//价钱
	private void readyOrder(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// TODO Auto-generated method stub
		Userinfo userinfo=(Userinfo)request.getSession().getAttribute("userinfo");

		String name=request.getParameter("username");
		String phone=request.getParameter("phone");	
		
		String startTime=request.getParameter("startTime");
		String endTime=request.getParameter("endTime");
		String time=request.getParameter("time");
		String roomType=request.getParameter("roomType");
		String breakfast = request.getParameter("breakfast");
		boolean f=breakfast.equals("不带早餐")?false:true;
		String car=request.getParameter("car");
		time=time.replace("T", " ");
		boolean ff=car.equals("false")?false:true;
		
		DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
		DateFormat timeDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm");
		Date st=null;
		Date et=null;
		Date tt=null;
		
		try {
			st=dateFormat.parse(startTime);
		} catch (ParseException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
			et=dateFormat.parse(endTime);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
		}
		try {
			tt=timeDateFormat.parse(time);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
		}
		Timestamp tst=new Timestamp(st.getTime());
		Timestamp tet=new Timestamp(et.getTime());
		Timestamp ttt=null;
		Timestamp tttt=null;
		if(ff) {
			//订车
			ttt=new Timestamp(tt.getTime());
			Calendar carCalendar = Calendar.getInstance();
			carCalendar.setTime(ttt);
			carCalendar.add(carCalendar.HOUR, 2);
			tttt = new Timestamp(carCalendar.getTimeInMillis());
		}else {
			//不订车
			time=null;
		}
//		获得入住的每一天,放到list里
		List<Timestamp> timestamps=new ArrayList<Timestamp>();
		while(true) {
			if(tst.before(tet)) {
				timestamps.add(tst);
				Calendar startCalendar = Calendar.getInstance();
				startCalendar.setTime(tst);
				startCalendar.add(startCalendar.DATE, 1);
				tst = new Timestamp(startCalendar.getTimeInMillis());
			}else {
				break;
			}
		}
		
		PrintWriter out= response.getWriter();
		Boolean flag=RoomTypeDAO.getRoomtypedao().search(roomType,timestamps);
		Boolean carflag=true;
		if(ff) {//订车
			carflag=CarDAO.getCarDAO().search(ttt,tttt);
		}
		if(!carflag) {
			out.print("c");
		}else {
			if(flag) {//有空房
				//OrderDAO.getOrderdao().order(userinfo,name,phone,startTime,endTime,roomType,f,ff,time,tttt);
//				Double price =OrderDAO.getOrderdao().getPrice();
				Breakfast b=BreakfastDAO.getBreakfastdao().findall();
				Double carPrice=CarDAO.getCarDAO().findprice();
				Double discount=UserinfoDAO.getUserinfodao().findDiscountByUserinfoId(userinfo.getId());
				Double roomTypePrice= RoomTypeDAO.getRoomtypedao().getPriceByRoomTypeTitle(roomType);
				
				double totalPrice =0;
				if(f) {
					totalPrice+=b.getPrice()*timestamps.size();
				}
				if(ff) {
					totalPrice+=carPrice;
				}
				
				out.print((totalPrice+roomTypePrice*timestamps.size())*discount);
			}else {
				out.print("f");
			}
		}
		out.close();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	
	protected void comment(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		PrintWriter print = response.getWriter();
		int id=Integer.parseInt(request.getParameter("id"));
		boolean b=OrderDAO.getOrderdao().comment(id);
		
		if(b) {
			print.print("ok");
			
		}
		else {
			print.print("no");
		}
	}
	protected void tui(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		int id=Integer.parseInt(request.getParameter("id"));
	OrderDAO.getOrderdao().updataTui(id);
		
	}
	protected void myOrder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		
		
		List<Order> orders=new ArrayList<Order>();
		Userinfo userinfo=(Userinfo)request.getSession().getAttribute("userinfo");
		 orders=OrderDAO.getOrderdao().findAllOrder(userinfo);
		List<Double> prices=new ArrayList<Double>();
		for(Order order:orders) {
			//Double prices=OrderDAO.getOrderdao().getPrice(order);
			Boolean bflag = order.getBreakfast();
			Boolean carflag = order.getCar();
			
			Breakfast b=BreakfastDAO.getBreakfastdao().findall();
			Double carPrice=CarDAO.getCarDAO().findprice();
			Double discount=UserinfoDAO.getUserinfodao().findDiscountByUserinfoId(userinfo.getId());
			Double roomTypePrice= RoomTypeDAO.getRoomtypedao().getPriceByRoomTypeTitle(order.getRoomType().getTitle());
			int days=OrderDAO.getOrderdao().getDays(order.getId());
			double totalPrice =0;
			if(bflag) {
				totalPrice+=b.getPrice()*days;
			}
			if(carflag) {
				totalPrice+=carPrice;
			}
			
			totalPrice=(totalPrice+roomTypePrice*days)*discount;
			
			BigDecimal   bbbb   =   new   BigDecimal(totalPrice); 
			totalPrice   =   bbbb.setScale(2,   BigDecimal.ROUND_HALF_UP).doubleValue(); 
			
			prices.add(totalPrice);
		}
		request.setAttribute("prices", prices);
		request.setAttribute("orders", orders);
		
		
		
		request.getRequestDispatcher("/FrontDesk/center/myOrder.jsp").forward(request, response);
		
	
	}
	protected void order(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
	Userinfo userinfo=(Userinfo)request.getSession().getAttribute("userinfo");

	String name=request.getParameter("username");
	String phone=request.getParameter("phone");	
	
	String startTime=request.getParameter("startTime");
	String endTime=request.getParameter("endTime");
	String time=request.getParameter("time");
	String roomType=request.getParameter("roomType");
	String breakfast = request.getParameter("breakfast");
	boolean f=breakfast.equals("不带早餐")?false:true;
	String car=request.getParameter("car");
	time=time.replace("T", " ");
	boolean ff=car.equals("false")?false:true;
	
	DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
	DateFormat timeDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm");
	Date st=null;
	Date et=null;
	Date tt=null;
	
	try {
		st=dateFormat.parse(startTime);
	} catch (ParseException e1) {
		// TODO Auto-generated catch block
		e1.printStackTrace();
	}
	try {
		et=dateFormat.parse(endTime);
	} catch (ParseException e) {
		// TODO Auto-generated catch block
	}
	try {
		tt=timeDateFormat.parse(time);
	} catch (ParseException e) {
		// TODO Auto-generated catch block
	}
	Timestamp tst=new Timestamp(st.getTime());
	Timestamp tet=new Timestamp(et.getTime());
	Timestamp ttt=null;
	Timestamp tttt=null;
	if(ff) {
		//订车
		ttt=new Timestamp(tt.getTime());
		Calendar carCalendar = Calendar.getInstance();
		carCalendar.setTime(ttt);
		carCalendar.add(carCalendar.HOUR, 2);
		tttt = new Timestamp(carCalendar.getTimeInMillis());
	}else {
		//不订车
		time=null;
	}
//	获得入住的每一天,放到list里
	List<Timestamp> timestamps=new ArrayList<Timestamp>();
	while(true) {
		if(tst.before(tet)) {
			timestamps.add(tst);
			Calendar startCalendar = Calendar.getInstance();
			startCalendar.setTime(tst);
			startCalendar.add(startCalendar.DATE, 1);
			tst = new Timestamp(startCalendar.getTimeInMillis());
		}else {
			break;
		}
	}
	
	PrintWriter out= response.getWriter();
	Boolean flag=RoomTypeDAO.getRoomtypedao().search(roomType,timestamps);
	Boolean carflag=true;
	if(ff) {//订车
		carflag=CarDAO.getCarDAO().search(ttt,tttt);
	}
	if(!carflag) {
		out.print("c");
	}else {
		if(flag) {//有空房
			OrderDAO.getOrderdao().order(userinfo,name,phone,startTime,endTime,roomType,f,ff,time,tttt);
			out.print("t");
		}else {
			out.print("f");
		}
	}
	out.close();
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
